package com.example.nio.im;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        new Thread(()->{
           while (true){
               try {
                   Socket socket = serverSocket.accept();
                   new Thread(() -> {
                       try {
                           int len;
                           byte[] data = new byte[1024];
                           InputStream inputStream = socket.getInputStream();
                           // (3) 按字节流方式读取数据
                           while ((len = inputStream.read(data)) != -1) {
                               System.out.println(new String(data, 0, len));
                           }
                       } catch (IOException e) {

                       }
               }).start();
               } catch (IOException e) {
                   e.printStackTrace();
               }

           }
        }).start();
    }
}
